<template>
	<view>
		<!-- 自定义导航栏 -->
		<z-nav-bar transparentFixedFontColor="#FFF" type="transparentFixed" title=" "></z-nav-bar>
		<!-- 公共组件-每个页面必须引入 -->
		<public-module></public-module>
		<view class="h-bg" :style="'background-image: url('+headBgImg+');'">
			<view class="box-item flex column">
				<view class="t1">
					加入红娘，成就百万姻缘
				</view>
				<view class="t2">
					请如实填写下方信息
				</view>
			</view>
		</view>
		<!-- 内容 -->
		<view class="main">
			<!-- 切换栏 -->
			<view class="qiehuan flex align-item" v-if="!UserApplyInfoData">
				<view class="qiehuan-item flex align-item column">
					<view class="qiehuan-item-tit-com" :class="type == 1 ? 'qiehuan-item-tit-on' : 'qiehuan-item-tit'" @click="change(1)">
						个人
					</view>
					<view class="qiehuan-item-icon" v-if="type == 1"></view>
				</view>
				<view class="qiehuan-item flex align-item column">
					<view class="qiehuan-item-tit-com" :class="type == 0 ? 'qiehuan-item-tit-on' : 'qiehuan-item-tit'" @click="change(0)">
						机构
					</view>
					<view class="qiehuan-item-icon" v-if="type == 0"></view>
				</view>
			</view>
			
			<view class="qiehuan flex align-item" v-if="UserApplyInfoData">
				<view class="qiehuan-item flex align-item column">
					<view class="qiehuan-item-tit-com qiehuan-item-tit-on">
						{{ formData.is_personal == 1 ? '个人' : '机构' }}
					</view>
				</view>
			</view>
			
			<!-- 表单 -->
			<view class="biaodan">
				<uni-forms ref="form">
					<uni-forms-item label="机构名称" name="organization_name" v-if="type == 0">
						<input class="input" v-model="formData.organization_name" type="text" placeholder="请输入机构名称" placeholder-class="yangshi" :disabled="UserApplyInfoData" />
					</uni-forms-item>
					<uni-forms-item label="公司信用代码" name="credit_code" v-if="type == 0">
						<input class="input" v-model="formData.credit_code" type="text" placeholder="请输入公司信用代码" placeholder-class="yangshi" :disabled="UserApplyInfoData" />
					</uni-forms-item>
					<uni-forms-item label="简介" name="remarks" v-if="type == 0"></uni-forms-item>
					<view class="jianjie" v-if="type == 0">
						<textarea 
							class="text-area"
							@blur="bindTextAreaBlur"
							cursor="10px"
							placeholder="请输入公司简介…"
							:disabled="UserApplyInfoData"
							:value="formData.remarks"
							placeholder-style="font-size: 28rpx;color: #BEC6D3;font-weight:300;"/>
					</view>
					<uni-forms-item label="姓名" name="name">
						<input class="input" v-model="formData.name" type="text" placeholder="请输入姓名" placeholder-class="yangshi" :disabled="UserApplyInfoData"  />
					</uni-forms-item>
					<uni-forms-item label="电话" name="phone">
						<input class="input" v-model="formData.phone" type="text" placeholder="请输入电话" placeholder-class="yangshi" :disabled="UserApplyInfoData"  />
					</uni-forms-item>
					<uni-forms-item label="身份证号" name="id_number" v-if="type == 1">
						<input class="input" v-model="formData.id_number" type="text" placeholder="请输入身份证号"  placeholder-class="yangshi" :disabled="UserApplyInfoData" />
					</uni-forms-item>
					<uni-forms-item label="微信号" name="wechat_number" v-if="type == 1">
						<input class="input" v-model="formData.wechat_number" type="text" placeholder="请输入微信号"  placeholder-class="yangshi" :disabled="UserApplyInfoData" />
					</uni-forms-item>
					
					<uni-forms-item label="市区" v-if="UserApplyInfoData">
						<input class="input" v-model="formData.city" type="text" placeholder="请选择市区"  placeholder-class="yangshi" disabled=""/>
					</uni-forms-item>
					
					<uni-forms-item label="市区" v-if="!UserApplyInfoData">
						<input class="input" v-model="formData.city" type="text" placeholder="请选择市区"  placeholder-class="yangshi" disabled="" @click="openPicker"/>
					</uni-forms-item>
					
					<uni-forms-item label="地址" name="address" class="dizhi">
						<input class="input" v-model="formData.address" type="text" placeholder="请输入详细地址"  placeholder-class="yangshi" :disabled="UserApplyInfoData" />
					</uni-forms-item>
					<!-- 上传logo -->
					<view class="logo" v-if="type == 0">
						<view class="logo-text">
							logo
						</view>
						
						<view class="grid col-3 grid-square flex-sub" v-if="!UserApplyInfoData">
							<view class="bg-img" @tap="ViewImage" :data-url="formData.logo" v-if="formData.logo != ''">
								<image :src="formData.logo" mode="aspectFill"></image>
								<view class="cu-tag bg-red" @tap.stop="DelImg()">
									<text class='cuIcon-close'></text>
								</view>
							</view>
							<view class="addimg solids" @tap="ChooseImage" v-if="formData.logo == ''">
								<text class='cuIcon-add' style="color: #7F5AFF;"></text>
							</view>
						</view>
						
						<view class="grid col-3 grid-square flex-sub" v-if="UserApplyInfoData">
							<view class="bg-img" @tap="ViewImage" :data-url="formData.logo">
								<image :src="formData.logo" mode="aspectFill"></image>
							</view>
						</view>
						
					</view>
					
					<!-- <view class="flex align-item shenhe-status" v-if="UserApplyInfoData">
						
						<view class="">
							{{ formData.status == 0 ? '待审核' : formData.status == 1 ? '已通过' : '已拒绝' }}
						</view>
					</view> -->
					
				</uni-forms>
				<view class="" v-if="UserApplyInfoData">
					<button class="btn btn1" :class="formData.is_personal == 1 ? 'mt-60' : ''" v-if="formData.status == 2" @click="UserApplyInfoData=false">(已拒绝)重新发布</button>
					<button class="btn btn1" :class="formData.is_personal == 1 ? 'mt-60' : ''" v-else>{{ formData.status == 0 ? '待审核' : formData.status == 1 ? '已通过' : '已拒绝' }}</button>
				</view>
				<view class="" v-else>
					<button class="btn btn1" :class="type == 1 ? 'mt-180' : ''" v-if="formData.name == '' || formData.phone == '' || formData.city == '' ||  formData.address == ''">确认发布</button>
					<button class="btn btn2" :class="type == 1 ? 'mt-180' : ''" @click="submit"  v-else>确认发布</button>
				</view>
				
			</view>
			
		</view>
		<view class="bottom-bg"></view>
		<view class="">
			<lotus-address v-on:choseVal="choseValue" :lotusAddressData="lotusAddressData"></lotus-address>
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex';
	import lotusAddress from "../../../components/lotusAddress/Winglau14-lotusAddress.vue";
	export default {
		data() {
			return {
				imgUrl: this.$http.baseUrl, // 服务器域名
				headBgImg: this.$http.baseUrl + 'static/web/img/apply-bg.png', //顶部背景图片 
				uid: '', // 用户id
				type: 1, // 切换栏
				// 表单数据
				formData: {
					name: '',
					phone: '',
					id_number: '',
					wechat_number: '',
					city: '',
					address: '',
					organization_name: '', // 机构名称
					credit_code: '', //公司信用代码
					remarks: '', // 公司简介
					is_personal: 1, // 是否是个人
					logo: '', // logo
				},
				// 省市区
				lotusAddressData:{
					visible:false,
					provinceName:'',
					cityName:'',
					townName:'',
				},
				UserApplyInfoData: false, // 用户是否已提交过认证信息
			}
		},
		computed: {
			...mapState(['userInfo']),
		},
		components:{
			"lotus-address":lotusAddress
		},
		methods: {
			// 切换tab栏
			change(i){
				if(i == 1){
					this.formData.logo = ''
					this.formData.organization_name = ''
					this.formData.credit_code = ''
					this.formData.remarks = ''
				}else if(i == 0){
					this.formData.id_number = ''
					this.formData.wechat_number = ''
				}
				this.type = i
				this.formData.is_personal = i
			},
			submit() {
				var that = this
				if(that.formData.name == ''){
					uni.showToast({
						icon: 'none',
						title: '请输入姓名'
					})
					return false
				}
				if(!uni.$u.test.mobile(that.formData.phone)){
					uni.showToast({
						icon: 'none',
						title: '请输入电话'
					})
					return false
				}
				if(that.formData.city == ''){
					uni.showToast({
						icon: 'none',
						title: '请选择市区'
					})
					return false
				}
				if(that.formData.address == ''){
					uni.showToast({
						icon: 'none',
						title: '请输入详细地址'
					})
					return false
				}
				if(that.type == 1){
					if(that.formData.id_number == ''){
						uni.showToast({
							title: '请输入身份证号',
							icon: 'none'
						})
						return false
					}
					if(that.formData.wechat_number == ''){
						uni.showToast({
							title: '请输入微信号',
							icon: 'none'
						})
						return false
					}
				}
				if(that.type == 0){
					if(that.formData.organization_name == ''){
						uni.showToast({
							title: '请输入机构名称',
							icon: 'none'
						})
						return false
					}
					if(that.formData.credit_code == ''){
						uni.showToast({
							title: '请输入公司信用代码',
							icon: 'none'
						})
						return false
					}
					if(that.formData.remarks == ''){
						uni.showToast({
							title: '请输入公司简介',
							icon: 'none'
						})
						return false
					}
					if(that.formData.logo == 0){
						uni.showToast({
							title: '请上传logo',
							icon: 'none'
						})
						return false
					}
				}
				that.$http.post('api/ZhMatchmaker/add',{
					...that.formData,'uid':that.uid,
				}).then(res => {
						if(res){
							uni.showToast({
								title: '发布成功,待审核',
								icon: 'none',
								success: res => {
									uni.navigateBack();
								}
							})
						}
					});
				
			},
			// 上传logo
			ChooseImage() {
				this.$http.urlImgUpload({
					 count:1
					}).then(e=>{
						
						// #ifdef MP-WEIXIN
						this.formData.logo = e[0]
						// #endif
						
						// #ifdef APP-PLUS || H5
						this.formData.logo = e
						// #endif
					})
			},
			ViewImage(e) {
				uni.previewImage({
					urls: [this.formData.logo],
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(){
				this.formData.logo = ''
			},
			//文本域
			 bindTextAreaBlur(e) {
				this.formData.remarks = e.detail.value
			},
			// 选择市区
			addressChange(e){
				
			},
			//打开picker
			openPicker() {
				this.lotusAddressData.visible = true;
			},
			//回传已选的省市区的值
			choseValue(res){
				//res数据源包括已选省市区与省市区code
				console.log(res);
				this.lotusAddressData.visible = res.visible;//visible为显示与关闭组件标识true显示false隐藏
				//res.isChose = 1省市区已选 res.isChose = 0;未选
				if(res.isChose){
					this.lotusAddressData.provinceName = res.province;//省
					this.lotusAddressData.cityName = res.city;//市
					this.lotusAddressData.townName = res.town;//区
					this.formData.city = `${res.province} ${res.city} ${res.town}`; //region为已选的省市区的值
				}
			},
			// 获取申请红娘信息
			getUserApplyInfo(){
				var that = this
				that.$http.get('api/ZhMatchmaker/view',{
					'uid':that.uid,
				}).then(res => {
						if(res){
							that.formData = res
							that.UserApplyInfoData = true
							that.type = res.is_personal
						}
					});
			}
		},
		onLoad() {
			var userInfo = this.$store.state.userInfo;
			this.uid = userInfo.uid
			if(userInfo.uid){
				this.getUserApplyInfo()
			}else{
				uni.showToast({
				    title: '请前往授权登录',
				    duration: 1000
				});
				setTimeout(function() {
					uni.switchTab({
						url: '/pages/my/my'
					});
				}, 1000);
			}
		}
	}
</script>

<style lang="scss">
page {
	background-color: #FFFFFF;
}
.uni-forms-item__label {
	width: 200rpx !important;
}
.uni-forms-item__content {
	display: flex;
	align-items: center;
	justify-content: flex-end;
	text-align: right;
}
.uni-forms-item {
	border-top: 1rpx solid #F9F9F9;
}
.dizhi > .uni-forms-item {
	border-bottom: 1rpx solid #F9F9F9 !important;
}
.uni-forms-item__inner {
	padding: 20rpx 0 !important;
}
.is-direction-left > .uni-forms-item__label .label-text {
	color: #6A6A7E !important;
	font-size: 28rpx !important;
}
.yangshi {
	font-size: 28rpx;
	font-family: PingFangSC-Light, PingFang SC;
	font-weight: 300;
	color: #BEC6D3;
}
.text-area {
	width: 100%;
	height: 160rpx;
	background: #F9F9F9;
	border-radius: 24rpx;
	padding: 20rpx;
}
.jianjie {
	margin-bottom: 16rpx;
}
.flex {
	display: flex;
}
.align-item {
	align-items: center;
}
.j-between {
	justify-content: space-between;
}
.column {
	flex-direction: column;
}
.h-bg {
	width: 100%;
	padding: 210rpx 0 64rpx;
	// #ifdef H5
	padding: 100rpx 0 64rpx;
	// #endif
	.box-item {
		margin-left: 30rpx;
		.t1 {
			font-size: 40rpx;
			font-family: SweiMarkerLegCJKsc-Bold, SweiMarkerLegCJKsc;
			font-weight: bold;
			color: #000013;
			line-height: 48rpx;
			margin-bottom: 16rpx;
		}
		.t2 {
			font-size: 26rpx;
			font-family: PingFangSC-Light, PingFang SC;
			font-weight: 300;
			color: #6A6A7E;
			line-height: 37rpx;
		}
	}
}
.main {
	width: 100%;
	padding: 30rpx;
	margin-top: -24rpx;
	border-radius: 24rpx;
	background: #FFFFFF;
	.qiehuan {
		.qiehuan-item {
			margin-right: 64rpx;
			.qiehuan-item-tit-com {
				display: flex;
				align-items: flex-end;
				font-family: PingFangSC-Light, PingFang SC;
				height: 56rpx;
				margin-bottom: 20rpx;
				vertical-align: baseline;
			}
			.qiehuan-item-tit {
				font-size: 28rpx;
				font-weight: 300;
				color: #6A6A7E;
			}
			.qiehuan-item-tit-on {
				font-size: 40rpx;
				font-weight: 500;
				color: #000013;
			}
			.qiehuan-item-icon {
				width: 20rpx;
				height: 10rpx;
				background: #7F5AFF;
				border-radius: 10rpx 10rpx 0 0;
			}
		}
	}
	.biaodan {
		margin-top: 30rpx;
		.logo {
			margin-bottom: 40rpx;
			.logo-text {
				font-size: 28rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #6A6A7E;
				line-height: 40rpx;
				margin: 24rpx 0;
			}
		}
		.shenhe-status {
			justify-content: center;
			width: 100%;
			height: 120rpx;
			line-height: 120rpx;
			font-size: 32rpx;
			font-family: PingFangSC-Light, PingFang SC;
			font-weight: 500;
			color: #000013;
			.shenhe-status-tit {
				color: #6A6A7E;
			}
		}
		.btn {
			width: 620rpx;
			height: 80rpx;
			border-radius: 16rpx;
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			margin: 0 auto;
			z-index: 10;
		}
		.btn1 {
			background: #BEC6D3;
		}
		.btn2 {
			background: #7F5AFF;
		}
		.mt-180 {
			margin-top: 180rpx;
		}
		.mt-60 {
			margin-top: 60rpx;
		}
	}
}
.bottom-bg {
	position: fixed;
	bottom: 0;
	right: 0;
	width: 318rpx;
	height: 200rpx;
	background-image: url('https://dating.mync.xyz/static/web/img/apply-bg-b.png');
	background-size: 100% 100%;
}
</style>
